#!/usr/bin/perl -w

use strict;

my ($album, $artist, $price); 
my $total = 0;

format =
  @<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<< $@###.##
  dotize(17, $album), dotize(16, $artist), $price
.

format STDOUT_TOTAL = 
  ---------------------------------------------
                                       $@###.##
                                        $total
.

format STDOUT_TOP = 
  @|||||||||||||||||||||||||||||||||||||| Pg @<
  "CD Collection of YL",                     $%

  Album                Artist          Price
  ------------------   ---------------  ----------
.

$= = 6;
#$^L = '-' x 60 . "\n" . 
#      "Copyright, 2011, Amazon.com\n" .
#      "\n\n";

while (<STDIN>) {
   chomp;
   ($album, $artist, $price) = split(/!/);
# map -- not good, use for instead of map for in place processing of a list
#   ($album, $artist, $price) = map 
#        {defined($_) ? $_ : "" } ($album, $artist, $price);

# for -- not as good.  for price we want to define it as 0
#   for my $v ($album, $artist, $price) {
#      $v = "" unless defined $v;
#   }

   $album = "" unless defined($album);
   $artist = "" unless defined($artist);
   $price = 0 unless defined($price);
   $total = $total + $price;

   write();
}

#print ("\n" x $- . "$^L"); 

$~ = "STDOUT_TOTAL";
write();

sub dotize {
    my ($width, $string) = @_;
    if (length($string) > $width) {
        return(substr($string, 0, $width - 3) . "...");
    } else {
        return($string);
    }
}

